package com.quanta.edaily.controller;

import com.alibaba.fastjson.JSONObject;
import com.quanta.edaily.Service.CommentsService;
import com.quanta.edaily.bean.JsonResponse;
import com.quanta.edaily.bean.contants.ResultCode;
import com.quanta.edaily.pojo.Comments;
import com.quanta.edaily.utils.DateTimeUtils;
import com.quanta.edaily.utils.TokenUtils;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import static io.micrometer.common.util.StringUtils.*;

@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Resource
    CommentsService commentsService;

    @PostMapping("/first")
    public JsonResponse first(@RequestBody JSONObject jsonObject) {
        String content = jsonObject.getString("content");
        Integer articleId = jsonObject.getInteger("articleId");
        if(isEmpty(content)) return JsonResponse.paramError("参数错误");

        Comments comments = new Comments();
        comments.setContent(content);
        comments.setArticleId(articleId==null?0:articleId);
        comments.setUserId(TokenUtils.getCurrentUser().getId());
        if (commentsService.insertFirst(comments) == 1) return JsonResponse.success("插入成功");
        else return JsonResponse.error(ResultCode.SERVICE_ERROR, "插入失败");
    }

    @PostMapping("/reply")
    public JsonResponse reply(@RequestBody JSONObject jsonObject) {
        String content = jsonObject.getString("content");
        Integer parentId = jsonObject.getInteger("parentId");
        if(isEmpty(content)||parentId==null) return JsonResponse.paramError("参数错误");

        Comments comments = new Comments();
        comments.setContent(content);
        comments.setParentId(parentId);
        if (commentsService.insertReply(comments) == 1) return JsonResponse.success("插入成功");
        else return JsonResponse.error(ResultCode.SERVICE_ERROR, "插入失败");
    }

    @PostMapping("/deleteById/{id}")
    public JsonResponse deleteById(@PathVariable Integer id) {
//        Comments comments = commentsService.getById(id);
//        if(comments==null||!Objects.equals(TokenUtils.getCurrentUser().getId(), comments.getUserId()))
//            return JsonResponse.paramError("参数错误");
        if (commentsService.deleteById(id) == 1) return JsonResponse.success("删除成功");
        else return JsonResponse.error(ResultCode.SERVICE_ERROR, "删除失败");
    }

    @PostMapping("/updateById")
    public JsonResponse updateById(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        String content = jsonObject.getString("content");
        if(id!=null&&!isEmpty(content)) {
            Comments c1 = commentsService.getById(id);
            if(c1!=null&&TokenUtils.getCurrentUser().getId().equals(c1.getUserId())) {
                c1.setContent(content);
                c1.setUpdateTime(DateTimeUtils.genDateTime()); // 确保调用接口再更新
                if (commentsService.updateById(c1) == 1) return JsonResponse.success("更改成功");
                else return JsonResponse.error(ResultCode.SERVICE_ERROR, "更改失败");
            }
        }
        return JsonResponse.paramError("参数错误");
    }

    @GetMapping("/getById/{id}")
    public JsonResponse getById(@PathVariable Integer id) {
        Comments comments = commentsService.getById(id);
        if(comments ==null) return JsonResponse.paramError("查询失败");
        return JsonResponse.success("查询成功", comments);
    }

    @GetMapping("/getSome")
    public JsonResponse getSome(@Nullable @RequestParam Integer articleId) {
        return JsonResponse.success("查询成功", commentsService.getSome(articleId));
    }
}
